/**
 * Created by Administrator on 2017/3/4.
 */

// addLoadEvent(positionMessage);
var movement;
function positionMessage() {
    if (!document.getElementById) return false;
    if (!document.getElementById("message")) return false;
    var message = document.getElementById("message");
    message.style.position = 'absolute';
    message.style.left = '50px';
    message.style.top = '100px';

    setInterval("moveMessage('message', 200, 200, 1000)", 1000);
}

function moveMessage(elementID, final_x, final_y, interval) {
    if (!document.getElementById) return false;
    if (!document.getElementById(elementID)) return false;
    var message = document.getElementById(elementID);

    if (message.movement) {
        clearTimeout(message.movement);
    }
    if (!message.style.left) {
        message.style.left = '0px';
    }
    if (!message.style.top) {
        message.style.top = '0px';
    }

    var xpos = parseInt(message.style.left);
    var ypos = parseInt(message.style.top);
    var dist = 0;
    if (xpos == final_x && ypos == final_y) {
        return true;
    }
    if (xpos < final_x) {
        dist = Math.ceil((final_x - xpos) / 10)
        xpos += dist;
    }
    if (xpos > final_x) {
        dist = Math.ceil((xpos - final_x) / 10)
        xpos -= dist;
    }
    if (ypos < final_y) {
        dist = Math.ceil((final_y - ypos) / 10)
        ypos += dist;
    }
    if (ypos > final_y) {
        dist = Math.ceil((ypos - final_y) / 10)
        ypos -= dist;
    }
    // left += 50;
    // if (left >= window.screen.width) {
    //     left -= window.screen.width;
    // }
    // top += 50;
    // if (top >= window.screen.height) {
    //     top -= window.screen.height
    // }
    message.style.left = xpos + 'px';
    message.style.top = ypos + 'px';
    message.movement = setTimeout("moveMessage('"+elementID+"','"+final_x+"','"+final_y+"', '"+interval+"')", interval)
}

addLoadEvent(prepareSlideshow)
function prepareSlideshow() {
    if (!document.getElementsByTagName) return false;
    if (!document.getElementById) return false;

    if (!document.getElementById("linklist")) return false;
    // if (!document.getElementById("preview")) return false;

    var slideshow = document.createElement("div");
    slideshow.setAttribute("id", "slideshow");
    // var preview = document.getElementById("preview");
    var preview = document.createElement("img");
    preview.setAttribute("id", "preview");
    preview.setAttribute("src", "images/topics.gif");
    preview.setAttribute("alt","building block of web design");
    // preview.style.position = "absolute";
    slideshow.appendChild(preview);

    // preview.style.left = '0px';
    // preview.style.top = '0px';


    var list = document.getElementById("linklist");
    insertAfter(slideshow, list);

    var links = list.getElementsByTagName("a");

    for (var i = 0; i < links.length; i++) {
        links[0].onmouseover = function () {
            moveMessage("preview", -100, 0, 10)
        }
        links[1].onmouseover = function () {
            moveMessage("preview", -200, 0, 10)
        }
    }

}
